package ua.amsoft.brelok.manager;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import ua.amsoft.brelok.db.DatabaseOpenHelper;
import ua.amsoft.brelok.util.DateTimeUtils;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

import com.google.common.io.ByteStreams;

public class DatabaseManagerTest extends AndroidTestCase {
    
    private DatabaseManager manager;
    private DatabaseOpenHelper databaseOpenHelper;
    
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        this.manager = new DatabaseManager(this.mContext);
        this.databaseOpenHelper = this.manager.getOpenHelper();
        this.manager.open();
        this.manager.getSqlLiteDatabase().beginTransaction();
    }
    
    public void testShouldCreateBrelokDatabaseManagerInstance() {
        final DatabaseManager managerInstance = new DatabaseManager(this.mContext);
        assertNotNull("manager instance should be not null", managerInstance);
    }
    
    public void testShouldOpenWritableDatabase() {
        final SQLiteDatabase database = this.manager.getSqlLiteDatabase();
        assertNotNull("database should be not null", database);
        assertTrue("database should be opened", database.isOpen());
        assertFalse("Databse should be writable", database.isReadOnly());
    }
    
    public void testShouldInsertRowToDepartamentUsageReportsTable() throws IOException {
        final long id = insertRecord();
        assertTrue(id > 0);
    }
    
    public void testShouldReturnCursorOfRows() throws IOException {
        
        insertRecord();
        final Cursor cursor = this.manager.selectDistinctLimit(false, "DepartamentUsageReports", null, null, null,
                null, null, null, "1");
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                final int id = cursor.getInt(cursor.getColumnIndex("dur_id"));
                final byte[] file = cursor.getBlob(cursor.getColumnIndex("dur_file"));
                final String date = cursor.getString(cursor.getColumnIndex("dur_updated_date"));
                assertNotNull(id);
                assertNotNull(file);
                assertEquals(getXMLFileBytes().length, file.length);
                assertNotNull(date);
                assertEquals("1970-01-01 00:00", date);
                cursor.moveToNext();
            }
        }
    }
    
    private long insertRecord() throws IOException {
        final ContentValues values = new ContentValues();
        values.put("dur_file", getXMLFileBytes());
        values.put("dur_updated_date", DateTimeUtils.formatInputDate(new Date(0L)));
        return this.manager.insert("DepartamentUsageReports", null, values);
    }
    
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        this.manager.getSqlLiteDatabase().endTransaction();
        this.manager.close();
        this.databaseOpenHelper.close();
    }
    
    private byte[] getXMLFileBytes() throws IOException {
        final InputStream inputStream = getContext().getAssets().open("dur.xml");
        return ByteStreams.toByteArray(inputStream);
    }
    
}
